<?php
class IndexController extends Zend_Controller_Action
{	
	/*
	 * Date: 2009.02.18
	 * Author:hanframe or 洪峰
	 * Email:hongfeng891027@163.com
	 * QQ:94526693
	 * Blog:http://hi.baidu.com/hanframe_ip
	 */
	function init()
	{	
	    $this->registry = Zend_Registry::getInstance();
	    $this->view = $this->registry['view'];
	    $this->view->baseUrl = $this->_request->getBaseUrl();
	}
   
	function indexAction() 
	{ 
		echo $this->view->render('fivechess/index.phtml');//显示模版  
	}
  
	function gameroomAction() 
	{ 
		$fivechess = new FiveChess();
		$user	 = new User();
		$filter=new Zend_Filter_StripTags();
		$your_id = $filter->filter(($this->_request->getParam('your_id')));
		if($this->_request->isPost())	{//user login in and will find a table to sit down
			//get all the record
			//get the user infomation by this id
			$this->view->user = $user->getUserByID($your_id);
			$this->view->users = $user->getAllUser();
			//get the numbers of tables
			$this->view->room_user = $fivechess->getRoom_join_User($your_id);
			$this->view->count = count($this->view->room_user);
			
			/*
			*window.showModalDialog('db.cgi?next_file=showwindow.htm\r\n','','dialogHeight:215px;dialogWidth:230px;help:       No;       resizable:       No;       status:       No;       scroll:no;       dialogTop:'+(screen.height-116)/2+'px;       dialogLeft:'+(screen.width-232)/2+'px;')
			*
			*
			*/
			echo $this->view->render('fivechess/gameroom.phtml');//显示模版  
		}
		else	{//ajax get url

			$method = $filter->filter(($this->_request->getParam('method')));
			$usertype = $filter->filter(($this->_request->getParam('usertype')));
			$table_id = $filter->filter(($this->_request->getParam('table')));
			$status = $filter->filter(($this->_request->getParam('status')));
			$chesstableone = $fivechess->getChessBordByID($table_id);
			$next_id = 0;
			if(($chesstableone['status'] == 3 && $status = 4) || ($chesstableone['status'] == 4 && $status = 3) )	{
				$status = 1;
				$next_id = $chesstableone['host_id'];
			}
			if($method == "set")	{
				$fivechess->setUser_ID_Zero('host',$your_id);
				$fivechess->setUser_ID_Zero('guest',$your_id);
				$data=array(
					'next_id'=>$next_id,
	    		$usertype.'_id'=>$your_id,
	    		'status'=>$status
					);
				$fivechess->updateChessBordByID($data,$table_id);
			}
			else if($method == "start")	{
				$status = 1;
				$data=array(
	    		'status'=>$status,
	    		'create_time'=>time(),
	    		'update_time'=>time()
					);
				$fivechess->updateChessBordByID($data,$id);
			}
			$gameroom = $fivechess->getGameRoom();
			$ret = "";
			foreach ($gameroom as $table)
			{
				
				$host_name = "";
				$host_face = 0;
				$guest_name = "";
				$guest_face = 0;
				//join all the infomation to a string.
				if($ret =="")
					$ret = $table['id']."|".$table['host_id']."|".$table['guest_id']."|".$table['status'];
				else
					$ret = $ret."!".$table['id']."|".$table['host_id']."|".$table['guest_id']."|".$table['status'];
			}
			echo $ret;
		}
	} 
	
	public function chessboardAction()
	{
			Zend_Loader::loadClass('Zend_Filter_StripTags');
			$filter=new Zend_Filter_StripTags();
		if($this->_request->isPost())	{
			
			//fetch the data from the form
			$id = $filter->filter(($this->_request->getParam('table')));
			$your_id = $filter->filter(($this->_request->getParam('your_id')));;
			$host_id = $filter->filter(($this->_request->getPost('host_id')));
			$guest_id = $filter->filter(($this->_request->getPost('guest_id')));
  		$next_id = $filter->filter($this->_request->getPost('host_id'));
  		$prev_step = 0;
  		$status = 1; //start game
  		
  		$chessboard = "";
			$fivechess = new FiveChess();
			//$db=$fivechess->getAdapter();
			//count the number which $id 
			$count = $fivechess->countByID($id);
					$data=array(
	    			'id'=>$id,
						'host_id'=>$host_id,
						'guest_id'=>$guest_id,
		    		'next_id'=>$next_id,
		    		'prev_step'=>$prev_step,
						'status'=>$status,
						'chessboard'=>$chessboard,
						'update_time'=>time()
						);
			$this->view->fivechess = $data;
			unset($data);
			//get the hostname and guestname
			$user = new User();
			$this->view->user_id = $your_id;
			$this->view->host_user = $user->getUserByID($host_id);
			$this->view->guest_user = $user->getUserByID($guest_id);
			
			//$webpage = "javascript:window.showModalDialog('fivechess/chessboard.phtml\r\n','','dialogHeight:215px;dialogWidth:230px;help:       No;       resizable:       No;       status:       No;       scroll:no;       dialogTop:'+(screen.height-116)/2+'px;       dialogLeft:'+(screen.width-232)/2+'px;');";
			//echo $this->view->render($webpage);
			echo $this->view->render('fivechess/chessboard.phtml');//show the fivechess bord 
		}
		else	{   
			$ret =  "NotUpdate";	//return value;
			//$user = new User();
			$id = $filter->filter(($this->_request->getParam('table')));
			$user = $filter->filter(($this->_request->getParam('user')));
			$seat = $filter->filter(($this->_request->getParam('seat')));
			$method = $filter->filter(($this->_request->getParam('method')));
			$fivechess = new FiveChess();
			$boardinfo = $fivechess->getChessBordByID($id);
			if($method == "get")	{
				if($boardinfo['status'] == 2 && $boardinfo['next_id'] == $user)	{
					$next_id = $boardinfo['host_id'];
					$status = 0;
					$seat = 0;
					$data=array(
	    		'next_id'=>$next_id,
	    		'prev_step'=>$seat,
	    		'status'=>$status,
					//'chessboard'=>$chessboard,
					'update_time'=>time()
					);
					$fivechess->updateChessBordByID($data,$id);
					$ret = "Lose|".$boardinfo['prev_step'];
					unset($data);
				}
				else if($boardinfo['next_id'] == $user && $boardinfo['prev_step'] != $seat && $boardinfo['prev_step'] != 0) 
					$ret = "Updated|".$boardinfo['prev_step'];
			}
			else	if($method == "set" || $method == "win"){
				$chessboard = $boardinfo['chessboard'];
				$chessboard = substr_replace($chessboard, $user, $seat - 1, 1);
				$next_id = ($user == $boardinfo['host_id']) ? $boardinfo['guest_id'] : $boardinfo['host_id'];
				if($method == "win")
					$status = 2;
				else
					$status = 1;
				$data=array(
	    		'next_id'=>$next_id,
	    		'prev_step'=>$seat,
	    		'status'=>$status,
					//'chessboard'=>$chessboard,
					'update_time'=>time()
					);
				$fivechess->updateChessBordByID($data,$id);
				unset($data);
			}
			echo $ret; 
		}
	}
}
	
	
	